<?php
include_once '../header1.php';
?>
<link rel="stylesheet" href="../css/datepicker.css" type="text/css" />
    <link rel="stylesheet" media="screen" type="text/css" href="../css/layout.css" />
  	<script type="text/javascript" src="../js/jquery.js"></script>
	<script type="text/javascript" src="../js/datepicker.js"></script>
    <script type="text/javascript" src="../js/eye.js"></script>
    <script type="text/javascript" src="../js/utils.js"></script>
    <script type="text/javascript" src="../js/layout.js?ver=1.0.2"></script>
  <script type="text/javascript">
function re_te()
{
document.getElementById("winner").innerHTML="Winner:</td><td><input type='text' name='Winner'/>";
}
function hide()
{
document.getElementById("winner").innerHTML="";
}

</script>
<script type="text/javascript">
function getHttpRequest()
{
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function showHint(str)
{
var xmlhttp;    
if (str=="")
  {
  document.getElementById("List").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   document.getElementById("List").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","upp_sch_names.php?q="+str,true);
xmlhttp.send();
}
function fill(val)
{
document.form1.SchName.value=val;
document.getElementById("List").innerHTML="";
var xmlhttp;    
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   document.getElementById("CompId").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","CIDupp.php?q="+val,true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
function getHttpRequest()
{
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function getComDate(com)
{
var xmlhttp;  
var sch=document.forms["form1"]["SchName"].value;
var hin=com+","+sch; 
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
   document.getElementById("Com_date").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_Com_date.php?q="+hin,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
include_once '../Login/databaseconnection.php';

if(!loggedin())
{
require_once '../index.php';
}
      else
   {
   date_default_timezone_set("Europe/Paris");
    $t_d=date("Y-m-d");
   if(isset($_POST['SaveChanges']))
   {
 if($_SESSION['Access_Type']=="Teacher in Charge")
{
$SchName=$_POST['SchName'];
$name=explode(",",$SchName);
$nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $School_Id=$row['School_Id'];
}
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
    $Own_School_Id=$rowsch['School_Id'];
  
   if($School_Id==$Own_School_Id)
	{
  require_once 'UPP.php';
   }
   }
   }
else if($_SESSION['Access_Type']=="Administrator")
{
require_once 'UPP.php';
}   
   else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
}
   $Competition_Id="";
   $School_Id="";
 $Date="";
   if(isset($_POST['Edit_This']))
	   {
	 $Competition_Name=$_POST['Competition_Name'];
	  $resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Name='$Competition_Name'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Competition_Id=$rownet['Competition_Id'];
}
$SchName=$_POST['SchName'];
$name=explode(",",$SchName);
$nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $School_Id=$row['School_Id'];
}
	  $Date=$_POST['Date'];
	  
	   $_SESSION['Competition_Id']=$Competition_Id;
   $_SESSION['School_Id']=$School_Id;
	  $_SESSION['Date']=$Date;
	  if($_SESSION['Access_Type']=="Teacher in Charge")
{
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];   
 if($Competition_Name=="Select One"||$SchName==""||$Date=="Select One")
	   {		    
	$_SESSION['errorin1']="Please select the Competition Name,School's Name and Date!";
	   }
	   else
	   {
	   if($School_Id==$Own_School_Id)
	{
	   $result=mysql_query("SELECT * FROM competition WHERE Competition_Id='$Competition_Id' AND School_Id='$School_Id' AND Date='$Date'");
	   if(mysql_num_rows($result)==0)
   {
	   $_SESSION['errornorow']="Very sorry there is no result,Please check your input and try again!";
   }
   }
   else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
	   }
}
 else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
}
 else if($_SESSION['Access_Type']=="Administrator")
{
	   if($Competition_Name=="Select One"||$SchName==""||$Date=="Select One")
	   {
		    
	$_SESSION['errorin1']="Please select the Competition Name,School's Name and Date!";

	   }
	   else
	   {
	   $result=mysql_query("SELECT * FROM competition WHERE Competition_Id='$Competition_Id' AND School_Id='$School_Id' AND Date='$Date'");
	   if(mysql_num_rows($result)==0)
   {

	   $_SESSION['errornorow']="Very sorry there is no result,Please check your input and try again!";
   }
   }
	   }
	   else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
	   }
   ?>
   <?php
include_once "../header2.php";
?>
<?php
include_once "topmenu.php";
?>
<div id="content">
	<div id="main">
	  <div id="bodyfield">
      <div id="lefttablereg">
      <form action="#" method="POST" name="form1">
  <table width="500" border="0" class="tablefontv">
    <tr>
      <td colspan="6"  scope="row" class="err"><?php
		if(isset($_POST['Edit_This'])||isset($_POST['SaveChanges']))
{
	if(isset($_SESSION['error']))
	{
	echo '<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['error'];
		unset($_SESSION['error']);
	}
	if(isset($_SESSION['errorin']))
	{
		echo '<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['errorin'];
		unset($_SESSION['errorin']);
	}
	if(isset($_SESSION['errorin1']))
	{
	echo '<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['errorin1'];
		unset($_SESSION['errorin1']);
	}
	if(isset($_SESSION['errornorow']))
	{
	echo '<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['errornorow'];
		unset($_SESSION['errornorow']);
	}
	  if(isset($_SESSION['errornotallo']))
	{
	echo '<img src="../images/alert.jpg" width="12" height="12"/>';
		echo $_SESSION['errornotallo'];
		unset($_SESSION['errornotallo']);
	}
}
		?>
      </td>
        
      </tr>
    <tr>
      <th width="29"  scope="row">&nbsp;</th>
      <th width="171"  scope="row" bgcolor="#6AB5FF">School's Name</th>
		<th width="22" height="22" align="left" scope="row" bgcolor="#6AB5FF">:-</th>
      <td width="139" bgcolor="#999999">
     <?php
	 	if($_SESSION['Access_Type']=="Teacher in Charge")
{
	$NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id']; 
    $snam=mysql_query("SELECT * FROM school WHERE School_Id='$Own_School_Id'");
	  if($ros=mysql_fetch_array($snam))
	  {
	  $SchName=$ros['Name'].','.$ros['Address'];
	  }
}
	 echo "<input type='text' id='SchName' name='SchName' onKeyUp='showHint(this.value)' value='$SchName' size='40' readonly='readonly'/>";
}
	else if($_SESSION['Access_Type']=="Administrator")
{
	if(!isset($SchName))
	{
		$snam=mysql_query("SELECT * FROM school WHERE School_Id='$School_Id1'");
	  if($ros=mysql_fetch_array($snam))
	  {
	  $SchName=$ros['Name'].','.$ros['Address'];
	  }
	}
	    echo "<input type='text' id='SchName' name='SchName' onKeyUp='showHint(this.value)' value='$SchName' size='40'/>";
}
	  ?>
     
     </td>
      <td width="106" bgcolor="#6AB5FF">&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <th scope="row">&nbsp;</th>
      <th scope="row" bgcolor="#6AB5FF">Competition Name</th>
	  <th height="22" align="left" scope="row" bgcolor="#6AB5FF">:-</th>
      <td bgcolor="#999999">
              <div id="CompId"><select name="Competition_Name" id="Competition_Name" onChange="getComDate(this.value)">
        <?php
	  if(isset($_POST['SaveChanges']))
	  {
	  $Comp_Id=$_SESSION['Comp_Id'];
	   $resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$Comp_Id'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Competition_Name=$rownet['Competition_Name'];
}
   	 echo '<option value="'.$Competition_Name.'">'.$Competition_Name.'</option>';
	  unset($_SESSION['Comp_Id']);
	  }
	  if(isset($_POST['Edit_This']))
	  {
	  $resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$Competition_Id'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Competition_Name=$rownet['Competition_Name'];
}
	 echo '<option value="'.$Competition_Name.'">'.$Competition_Name.'</option>';
	  }
	  else
	  {
	  echo '<option value="Select One">Select One</option>';
	  }
	  ?>
        <?php		
 $com=mysql_query("SELECT DISTINCT(Competition_Id) FROM competition");
 while($row=mysql_fetch_array($com))
  {
	 $Compe_Id=$row['Competition_Id'];
	  $resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Id='$Compe_Id'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Compe_Name=$rownet['Competition_Name'];
}
if($Competition_Name!=$Compe_Name)
{
	  echo '<option value="'.$Compe_Name.'">'.$Compe_Name.'</option>';
	  }
  }
 ?>
      </select></div>
      </td>
      <td bgcolor="#6AB5FF">&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <th scope="row">&nbsp;</th>
      <th scope="row" bgcolor="#6AB5FF">Date</th>
      <th height="22" align="left" scope="row" bgcolor="#6AB5FF">:-</th>
      <td bgcolor="#999999">
      <div id="Com_date">
      <select name="Date" id="Date">
       <?php
	  	   if(isset($_POST['SaveChanges']))
	  {
	  $Dat=$_SESSION['Dat'];
	   echo '<option value="'.$Dat.'">'.$Dat.'</option>';
	   unset($_SESSION['Dat']);
	  }
	  if(isset($_POST['Edit_This']))
	  {
	 echo '<option value="'.$Date.'">'.$Date.'</option>';
	  }
	  else
	  {
	  echo '<option value="Select One">Select One</option>';
	  }
			$com=mysql_query("SELECT DISTINCT(Date) FROM competition");
	while($row=mysql_fetch_array($com))
  {
	 $Dat=$row['Date'];
if($Date!=$Dat)
{
	  echo '<option value="'.$Dat.'">'.$Dat.'</option>';
	  }
  }
	  ?>
      </select> 
      </div></td>
      <td bgcolor="#6AB5FF">&nbsp;</td>
      <td>&nbsp;</td>
         </tr>
    <tr>
      <th scope="row">&nbsp;</th>
      <th height="24" scope="row" bgcolor="#6AB5FF"><a href="UpdateComSch.php" class="reset">Reset |</a>
        <input name="Edit_This" align="middle" type="submit" value="Edit this" class="buttonok"/></th>
      <td height="24" colspan="3" bgcolor="#6AB5FF" scope="row">
      </td>
      <td width="63">&nbsp;</td>
    </tr>
  </table>
  <br/>  
</form>
<form action="#" method="POST">
    <?php 
	if(isset($_POST['Edit_This']))
	   {
 $Competition_Name=$_POST['Competition_Name'];
	  $resultnet=mysql_query("SELECT * FROM competitions WHERE Competition_Name='$Competition_Name'");
  if($rownet=mysql_fetch_array($resultnet))
  {
		$Competition_Id=$rownet['Competition_Id'];
}
$SchName=$_POST['SchName'];
$name=explode(",",$SchName);
$nam=$name[0];
$tn=count($name);
for($i=1;$i<$tn;$i++)
{
if(empty($adr))
{
$adr=$name[$i];
}
else
{
$adr=$adr.",".$name[$i];
}
}
$results= mysql_query("SELECT * FROM school Where Name='$nam' AND Address='$adr'");
if($row = mysql_fetch_array($results))
{
 $School_Id=$row['School_Id'];
}
	  $Date=$_POST['Date'];
	   if($Competition_Name!="Select One"&&$SchName!=""&&$Date!="Select One")
	   {
$result=mysql_query("SELECT * FROM competition WHERE Competition_Id='$Competition_Id' AND School_Id='$School_Id' AND Date='$Date'");
  if(mysql_num_rows($result)>0)
  {
  echo '<table width="354" border="0" class="tablefontv">';
 while($row=mysql_fetch_array($result))
  {
   $Competition_Id=$row['Competition_Id'];
$School_Id=$row['School_Id'];
    $No_Of_Students=$row['No_Of_Students'];
    $No_Of_Teachers=$row['No_Of_Teachers'];
    $Result=$row['Result'];
    $Date=$row['Date'];
    $Scope=$row['Scope'];
   $Winner=$row['Winner'];
    if($_SESSION['Access_Type']=="Teacher in Charge")
{
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
  
   if($School_Id==$Own_School_Id)
	{
    echo '<tr>';
        echo '<td width="200" height="44" scope="row">';
		echo "Competition Id:";
		echo '</td>';
        echo '<td width="144">';
		echo "<input type='text' name='Competition_Id' value='$Competition_Id'>";
	echo '</td>';
	echo '<td height="44" scope="row">';
		echo "Scope:"; 
		echo '</td>';
        echo '<td>';
		echo "<input type='text' name='Scope' value='$Scope'>";
	
		echo '</td>';
		 
      echo '</tr>';
	  echo '<tr>';
        echo '<td height="44" scope="row">';
		echo "School Id:";
		echo '</td>';
        echo '<td>';
		echo "<input type='text' name='School_Id' value='$School_Id'>";
		echo '</td>';
		echo '<td height="44" scope="row">';
		echo "Number of teachers:";
		echo '</td>';
        echo '<td>';
		echo "<input type='text' name='No_Of_Teachers' value='$No_Of_Teachers'>";
	echo '</td>';
		
      echo '</tr>';
	  
      echo '<tr>';
        echo '<td height="44" scope="row">';
		echo "Date:";
		echo '</td>';
        echo '<td>';
		if(isset($Date))
		{
		echo "<input type='text' name='Date' value='$Date' class='inputDate' id='inputDate'>";
		}
		else
		{
		echo "<input type='text' name='Date' value='$t_d' class='inputDate' id='inputDate'>";
		}
	echo '</td>';
	  if($Result=="Win")
	  {
	 echo '<td height="44" scope="row">';
		echo "Winner:"; 
		echo '</td>';
        echo '<td>';
		echo "<input type='text' name='Winner' value='$Winner'>";
		echo '</td>';
				}
		else
	  {
	  echo '<td>';
	  ?>
	  <div id="winner">
	     </div>
		 <?php
		    echo '</td>';
	  }
      echo '</tr>';
      echo '<tr>';
        echo '<td height="54" scope="row">';
		echo "Number of students:";
		echo '</td>';
        echo '<td>';
		echo "<input type='text' name='No_Of_Students' value='$No_Of_Students'>";
		
		echo '</td>';
		     echo '<td height="44" scope="row">';
		echo "Result:";
		echo '</td>';
       echo '<td>';
 if($Result=="Win")
	  {
		echo '<input type="radio" name="Result" value="Win" id="Result" checked="checked" onClick="re_te()"/>';
	   echo "Win";
	  }
	  else
	  {
		  echo '<input type="radio" name="Result" value="Win" id="Result" onClick="re_te()"/>';
	   echo "Win";
	  }
        echo '<br />';
        if($Result=="Loss")
		{ 
		
          echo '<input type="radio" name="Result" value="Loss" id="Result" checked="checked" onClick="hide()"/>';
          echo "No";
  }
  else
  {
	   echo '<input type="radio" name="Result" value="Loss" id="Result" onClick="hide()"/>';
          echo "No";
  }
	  echo '</td>';
      echo '</tr>';
         }
     }
 }
 if($_SESSION['Access_Type']=="Administrator")
 {
  echo '<tr>';
        echo '<td width="200" height="44" scope="row">';
		echo "Competition Id:";
		echo '</td>';
        echo '<td width="144">';
		echo "<input type='text' name='Competition_Id' value='$Competition_Id'>";
	echo '</td>';
	echo '<td height="44" scope="row">';
		echo "Scope:"; 
		echo '</td>';
        echo '<td>';
		echo "<input type='text' name='Scope' value='$Scope'>";
	
		echo '</td>';
		 
      echo '</tr>';
	  echo '<tr>';
        echo '<td height="44" scope="row">';
		echo "School Id:";
		echo '</td>';
        echo '<td>';
		echo "<input type='text' name='School_Id' value='$School_Id'>";
		echo '</td>';
		echo '<td height="44" scope="row">';
		echo "Number of teachers:";
		echo '</td>';
        echo '<td>';
		echo "<input type='text' name='No_Of_Teachers' value='$No_Of_Teachers'>";
	echo '</td>';
		
      echo '</tr>';
	  
      echo '<tr>';
        echo '<td height="44" scope="row">';
		echo "Date:";
		echo '</td>';
        echo '<td>';
		if(isset($Date))
		{
		echo "<input type='text' name='Date' value='$Date' class='inputDate' id='inputDate'>";
		}
		else
		{
		echo "<input type='text' name='Date' value='$t_d' class='inputDate' id='inputDate'>";
		}
	echo '</td>';
	 echo '<td height="44" scope="row">';
	 	echo "Result:";
		echo '</td>';
       echo '<td>';
	   if($Result=="Win")
	  {
		echo '<input type="radio" name="Result" value="Win" id="Result" checked="checked" onClick="re_te()"/>';
	   echo "Win";
	  }
	  else
	  {
		  echo '<input type="radio" name="Result" value="Win" id="Result" onClick="re_te()"/>';
	   echo "Win";
	  }
        echo '<br />';
        if($Result=="Loss")
		{ 
		
          echo '<input type="radio" name="Result" value="Loss" id="Result" checked="checked" onClick="hide()"/>';
          echo "No";
  }
  else
  {
	   echo '<input type="radio" name="Result" value="Loss" id="Result" onClick="hide()"/>';
          echo "No";
  }
		echo '</td>';
      echo '</tr>';
      echo '<tr>';
        echo '<td height="54" scope="row">';
		echo "Number of students:";
		echo '</td>';
        echo '<td>';
		echo "<input type='text' name='No_Of_Students' value='$No_Of_Students'>";
		
		echo '</td>';
		  if($Result=="Win")
	  {
		     echo '<td height="44" scope="row">';
	echo "Winner:"; 
		echo '</td>';
        echo '<td>';
		echo "<input type='text' name='Winner' value='$Winner'>";
	echo '</td>';
	  }
	  else
	  {
	  echo '<td>';
	  ?>
	  <div id="winner">
	     </div>
		 <?php
		    echo '</td>';
	  }
	 
      echo '</tr>';
 }
 }
 echo '</table>';
      echo "<br/>";
   echo '<input name="SaveChanges" align="middle" type="submit" value="Save changes" class="buttonok"/>';
 }
 }
	   }
	   ?>
       </form>
       </div>
 <div id="righttablereg">
  <div class="suggestionListsch" id="List"></div>
 </div>
</div>
</div>
</div>
 <div id="sidebar"><br/>
<div id="partners" class="boxed">
			<h2 class="title">COMPETITIONS</h2>
            </div>
<?php
include_once "menu.php";
?>
 <?php
include_once "../footer.php";
?>
<?php

}
?>
</body>
</html>